package couponRecordService

import (
	"wowCoupon.com/models"
	"wowCoupon.com/pkg/tools"
)

type CouponRecordStatus int64

const (
	Ready CouponRecordStatus = iota
	Used
	Expired
)

type CouponRecord struct {
	ID           int64
	CouponMetaID int64
	UserId       string
	Status       int64
}

//To B, B adds new coupon record
func (c *CouponRecord) Add() error {
	coupon := map[string]interface{}{
		"coupon_meta_id": c.CouponMetaID,
		"user_id":        c.UserId,
		"status":         c.Status,
	}

	if err := models.AddCouponRecord(coupon); err != nil {
		return err
	}

	return nil
}

//To B, edit coupon record by id
func (c *CouponRecord) EditByID() error {
	coupon := tools.StructToMap(c)

	if err := models.EditCouponRecordByID(c.ID, coupon); err != nil {
		return err
	}

	return nil
}

//To B, edit coupon record by constraints
func (c *CouponRecord) Edit(constraints map[string]interface{}) error {
	data := tools.StructToMap(c)

	if err := models.EditCouponRecord(constraints, data); err != nil {
		return err
	}

	return nil
}

//To B, delete coupon record by id
func (c *CouponRecord) DeleteByID() error {
	if err := models.DeleteCouponRecordByID(c.ID); err != nil {
		return err
	}

	return nil
}

//To B, delete coupon record by constraints
func (c *CouponRecord) Delete(constraints map[string]interface{}) error {
	if err := models.DeleteCouponRecord(constraints); err != nil {
		return err
	}

	return nil
}
